﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Clarendon.AzureBilling.Library
{
    public class AzureServiceLoader
    {
        private readonly ServiceManager _manager = null;
        private readonly Semaphore _semaphore = null;

        public AzureServiceLoader()
        {
            _semaphore = new Semaphore(0, 3);
            _manager = new ServiceManager(_semaphore);
        }

        public void GetLoaded()
        {
            var request = new ServiceListRequester(_manager);
            request.MakeServiceRequest();
            _semaphore.WaitOne();

            var properties = new ServicePropertiesRequester(_manager);
            properties.MakeServiceRequest();
            _semaphore.WaitOne();

            var requester = new DeploymentsRequester(_manager);
            requester.MakeServiceRequest();
            requester.AzureHostedServiceListReturned += delegate(AzureResponseEventArgs args)
                                                            {
                                                                var loader = new AzureBillingService();
                                                                loader.UpdateServiceRequestAndCallbackSubscribers(
                                                                    args.HostedServices);
                                                            };
        }
    }
}
